﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;


namespace Sistema_de_Taller.SDT_BS
{
    public class WorkOrder_BS
    {
        public bool insert(Common_Files.Definitions.WorkOrder WorkOrder, DataRowCollection WorksDone, DataRowCollection Replacements, DataRowCollection AssignedEmployees, int tipo)
        {
            uint WorkOrderID = 0;
            Common_Files.Definitions.AssignedEmployee AssignedEmployee = new Common_Files.Definitions.AssignedEmployee();
            Common_Files.Definitions.WorkDone WorkDone = new Common_Files.Definitions.WorkDone();
            Common_Files.Definitions.ReplacementUsed ReplacementUsed = new Common_Files.Definitions.ReplacementUsed();
            Common_Files.Definitions.Remplacement Replacement = new Common_Files.Definitions.Remplacement();
            SDT_DAL.Utilities.WorkOrder_DAL inserto = new SDT_DAL.Utilities.WorkOrder_DAL();
            SDT_DAL.Utilities.Replacement_DAL UpdateStock = new SDT_DAL.Utilities.Replacement_DAL();
            SDT_DAL.Utilities.ReplacementUsed_DAL ReplacementInsert = new SDT_DAL.Utilities.ReplacementUsed_DAL();
            SDT_DAL.Utilities.WorkDone_DAL WorkDoneInsert = new SDT_DAL.Utilities.WorkDone_DAL();
            SDT_DAL.Utilities.AssignedEmployee_DAL AssignedEmployeeInsert = new SDT_DAL.Utilities.AssignedEmployee_DAL();

            if (tipo > 0)
                WorkOrderID = inserto.WorkOrderInsert(WorkOrder);
            else
            {
                inserto.WorkOrderUpdate(WorkOrder);
                WorkOrderID = WorkOrder.WorkOrder_ID;
            }
            
            foreach (DataRow WorkDoneFila in WorksDone)
            {
                WorkDone.WorkOrderID = WorkOrderID;
                WorkDone.WorkDone_Hours = Convert.ToUInt32(WorkDoneFila["Horas de trabajo"].ToString());
                WorkDone.WorkDone_Description = WorkDoneFila["Trabajo Realizado"].ToString();
                WorkDoneInsert.WorkDoneInsert(WorkDone);
            }

            foreach (DataRow ReplacementUsedFila in Replacements)
            {

                string ID = ReplacementUsedFila["ID / Nombre"].ToString();
                ReplacementUsed.WorkOrderID = WorkOrderID;
                ReplacementUsed.Replacement_ID = ID.Split(' ')[0];
                ReplacementUsed.ReplacementUsed_Quantity = Convert.ToUInt32(ReplacementUsedFila["Cantidad Utilizada"].ToString());
                ReplacementInsert.ReplacementUsedInsert(ReplacementUsed);
                Replacement.Replacement_ID = ID.Split(' ')[0];
                Replacement.Replacement_Stock = (UpdateStock.ReplacementActualStock(Replacement.Replacement_ID) - Convert.ToUInt32(ReplacementUsedFila["Cantidad Utilizada"].ToString()));
                Replacement.Replacement_Output = Convert.ToUInt32(ReplacementUsedFila["Cantidad Utilizada"].ToString());
                UpdateStock.ReplacementStockUpdate(Replacement);

            }

            foreach (DataRow AssignedEmployeeFila in AssignedEmployees)
            {
                string ID = AssignedEmployeeFila["ID / Nombre"].ToString();
                AssignedEmployee.WorkOrderID = WorkOrderID;
                AssignedEmployee.AssignedEmployee_Hours = Convert.ToUInt32(AssignedEmployeeFila["Horas Trabajadas"].ToString());
                AssignedEmployee.Employee_ID = Convert.ToUInt32(ID.Split(' ')[0].ToString());
                AssignedEmployeeInsert.AssignedEmployeeInsert(AssignedEmployee);
            }
            
            return true;
        }
    }
}